package com.wmlt.dao.impl;

import com.wmlt.util.JdbcUtil;
import com.wmlt.dao.MessageDao;
import com.wmlt.entity.Message;
import com.wmlt.entity.User;
import com.wmlt.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MessageDaoImpl extends JdbcUtil implements  MessageDao{
    int result=0;
    String sql="";
    Connection connection=null;
    PreparedStatement prea=null;
    ResultSet res=null;
    @Override
    public int sendMessage(Message message) {
        System.out.println(message);
        connection=JdbcUtil.getConnection();
        try{
            sql="insert into `message`(sender_user_id,addressee_user_id,message,sender_time) values (?,?,?,?)";
            prea=connection.prepareStatement(sql);
            prea.setString(1,message.getSenderUserId());
            prea.setString(2,message.getAddresseeUserId());
            prea.setString(3,message.getMessage());
            prea.setString(4,message.getSenderTime());
            result = prea.executeUpdate();
            System.out.println(result);
        }catch (Exception e){

        }finally {
            JdbcUtil.close(null,prea,null);
        }
        return result;
    }

    @Override
    public List<Message> getMessageListByUserId(String senderUserId,String addresseeUserId) {
        List<Message> messageList=null;
        connection=JdbcUtil.getConnection();
        try{
            sql="select username,user_img,user_id from User where user_id in(select addressee_user_id from `message`  as `msg`  where msg.addressee_user_id=? or msg.sender_user_id=?) or user_id in(select sender_user_id from `message`  as `msg`  where msg.addressee_user_id=? or msg.sender_user_id=?) or user_id=?";
            prea=connection.prepareStatement(sql);
            prea.setString(1,senderUserId);
            prea.setString(2,senderUserId);
            prea.setString(3,senderUserId);
            prea.setString(4,senderUserId);
            prea.setString(5,addresseeUserId);
            res=prea.executeQuery();
            if (res!=null){
                messageList=new ArrayList<Message>();
            }
            while (res.next()){
                User user = new User();
                user.setUserName(res.getString(1));
                user.setUserImg(res.getString(2));
                user.setUserId(res.getString(3));
                Message message = new Message();
                message.setUser(user);
                if (message.getUser().getUserId().equals(addresseeUserId)){
                    messageList.add(0,message);
                    continue;
                }
                messageList.add(message);
            }
            System.out.println(result);
        }catch (Exception e){

        }finally {
            JdbcUtil.close(res,prea,connection);
        }
        return messageList;
    }

    @Override
    public List<Message> getMessageRecord(String senderUserId, String addresseeUserId) {
        List<Message> messageList=null;
        connection=JdbcUtil.getConnection();
        try{
            sql="select sendUser.user_id, sendUser.username,sendUser.user_img,addresseeUser.user_id, addresseeUser.username,addresseeUser.user_img,message.message, message.sender_time from User sendUser,User addresseeUser,message where ((message.sender_user_id=? and message.addressee_user_id=?) or (message.sender_user_id=? and message.addressee_user_id=?)) and (sendUser.user_id=message.sender_user_id and addresseeUser.user_id=message.addressee_user_id) order by message.sender_time asc";
            prea=connection.prepareStatement(sql);
            prea.setString(1,senderUserId);
            prea.setString(2,addresseeUserId);
            prea.setString(3,addresseeUserId);
            prea.setString(4,senderUserId);
            res=prea.executeQuery();
            if (res!=null){
                messageList=new ArrayList<Message>();
            }
            while (res.next()){
                Message message = new Message();
                User sendUser = new User();
                sendUser.setUserId(res.getString(1));
                sendUser.setUserName(res.getString(2));
                sendUser.setUserImg(res.getString(3));
                User addresseeUser = new User();
                addresseeUser.setUserId(res.getString(4));
                addresseeUser.setUserName(res.getString(5));
                addresseeUser.setUserImg(res.getString(6));
                message.setMessage(res.getString(7));
                message.setSenderTime(res.getString(8));
                message.setSendUser(sendUser);
                message.setAddresseeUser(addresseeUser);
                if (message.getAddresseeUser().getUserId().equals(addresseeUserId)){
                    messageList.add(0,message);
                    continue;
                }
                messageList.add(message);
            }
            System.out.println(result);
        }catch (Exception e){

        }finally {
            JdbcUtil.close(res,prea,connection);
        }
        return messageList;
    }
}